package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.dao.FootXfDao;
import org.footballmanager.pandafootball.system.domain.FootXf;
import org.footballmanager.pandafootball.system.form.impl.FootXfSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootXfMapper;
import org.footballmanager.pandafootball.system.service.FootGpService;
import org.footballmanager.pandafootball.system.service.FootUserPersonService;
import org.footballmanager.pandafootball.system.service.FootXfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootXfServiceImpl extends ServiceImpl<FootXf> implements FootXfService {
    @Autowired
    private FootXfDao footXfDao;
    @Autowired
    private FootXfMapper footXfMapper;
    @Autowired
    private FootGpService footGpService;
    @Autowired
    private FootUserPersonService footUserPersonService;

    /**
     * 保存下放球员记录
     *
     * @param t       实体类
     * @param request 请求
     * @return 是否保存成功
     */
    @Override
    @Transactional
    public int save(FootXf t, HttpServletRequest request) {
        //删除挂牌信息
        footUserPersonService.deletePersonFromMyTeam(t.getUserId(), t.getQyId(), t.getXycs(), request);
        footGpService.deleteByQyidAndUserId(t.getQyId(), t.getUserId(), request);
        return super.save(t, request);
    }

    /**
     * 根据时间降序查找下放信息
     *
     * @param userId 用户主键
     * @return 下放球员列表
     */
    @Override
    public List<FootXf> findXfByTimeDesc(Long userId) {
        FootXfSearchForm footXfSearchForm = new FootXfSearchForm();
        footXfSearchForm.setUserId(userId);
        return this.findall(footXfSearchForm);
    }

    /**
     * 根据用户主键获取下放记录列表
     *
     * @param userId 用户主键
     * @return 下放记录列表
     */
    @Override
    public List<FootXf> findByUserId(Long userId) {
        FootXfSearchForm footXfSearchForm = new FootXfSearchForm();
        footXfSearchForm.setUserId(userId);
        return this.findall(footXfSearchForm);
    }

    /**
     * 翻页方法
     *
     * @param footXfSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootXf> findall(FootXfSearchForm footXfSearchForm) {
        return footXfMapper.findall(footXfSearchForm);
    }

    /**
     * 根据主键获取记录
     *
     * @param id 主键
     * @return 记录
     */
    @Override
    public FootXf get(Serializable id) {
        return footXfMapper.findById(id);
    }

    /**
     * 查询
     *
     * @param entityname       实体类名
     * @param map              参数
     * @param footXfSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootXfSearchForm footXfSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footXfSearchForm);
        Page<FootXf> page = PageHelper.startPage(footXfSearchForm.getPageIndex(), footXfSearchForm.getPageSize());
        List<FootXf> list = this.findall(footXfSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footXfSearchForm 条件
     * @param request          请求
     */
    @Override
    public void deleteAll(FootXfSearchForm footXfSearchForm, HttpServletRequest request) {
        if (footXfSearchForm.getSelectIds() != null) {
            String[] result = footXfSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 根据球员主键删除记录
     *
     * @param qyId 球员主键
     */
    @Override
    public void deletePersonByQyid(Long qyId) {
        Example example = new Example(FootXf.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("qyId", qyId);
        criteria.andEqualTo("delFlag", 0);
        List<FootXf> list = this.findall(example);
        if (list != null && !list.isEmpty()) {
            for (FootXf xf : list) {
                this.delete(xf.getId(), null);
            }
        }
    }
}
